﻿namespace ChinaUnicom.Models
{
    using System.Collections.Specialized;
    using System.ComponentModel;
    using Newtonsoft.Json;

    public class Item : INotifyPropertyChanged
    {
        public Item()
        {
            Details = new ItemCollection();
            Details.CollectionChanged += OnDetailsChanged;
        }

        string name;

        public string Name
        {
            get { return name; }
            set
            {
                if (value != name)
                {
                    name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        string value;

        public string Value
        {
            get { return value; }
            set
            {
                if (value != this.value)
                {
                    this.value = value;
                    NotifyPropertyChanged("Value");
                }
            }
        }

        public ItemCollection Details { get; private set; }

        [JsonIgnore]
        public bool HasDetails
        {
            get { return Details.Count > 0; }
        }

        public override string ToString()
        {
            return string.Format("{{{0} : {1}}}", Name, Value);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void NotifyPropertyChanged(string property)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(property));
            }
        }

        void OnDetailsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Details");
        }
    }
}
